package com.ayz.gateway.common.constants;

import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName ApiData
 * @Description TODO: 存储各个微服务向网关注册中心中注册的接口
 *              当前类存储Api接口的元数据信息
 * @Author 快乐的星球
 * @Date 2023/5/12 22:38
 * @Version 1.0
 **/
@Data
public class ApiData implements Serializable {
    /**
     * 应用程序的名称
     */
    private String applicationName;
    /**
     * Api接口的名称
     */
    private String interfaceName;
    /**
     * RPC框架注册中心的地址
     */
    private String rpcRegistryAddress;
    /**
     * 当前API接口是同步还是异步
     * TODO: 现在还不支持方法级别的同步和异步,默认是接口级别设置
     */
    private boolean isAsync;
    /**
     * 当前API接口调用的超时时间
     */
    private long timeout;
    /**
     * 当前接口的方法元数据信息
     */
    private List<MethodData> methodDataList;

    public ApiData(String applicationName, String interfaceName, String rpcRegistryAddress, boolean isAsync,
                   long timeout, List<MethodData> methodDataList) {
        this.applicationName = applicationName;
        this.interfaceName = interfaceName;
        this.rpcRegistryAddress = rpcRegistryAddress;
        this.isAsync = isAsync;
        this.timeout = timeout;
        this.methodDataList = methodDataList;
    }

    public ApiData(String applicationName, String interfaceName, String rpcRegistryAddress, boolean isAsync, long timeout) {
        this.applicationName = applicationName;
        this.interfaceName = interfaceName;
        this.rpcRegistryAddress = rpcRegistryAddress;
        this.isAsync = isAsync;
        this.timeout = timeout;
        this.methodDataList=new ArrayList<>();
    }

    public ApiData() {
        this.methodDataList=new ArrayList<>();
    }

    public void addMethodData(MethodData methodData){
        this.methodDataList.add(methodData);
    }

    public void addMethodDataList(List<MethodData> methodDataList){
        this.methodDataList.addAll(methodDataList);
    }

    /**
     * 生成API接口的唯一标识
     * @return
     */
    public String generateInterfaceId(){
        return applicationName+"@"+interfaceName;
    }

    public static String generateInterfaceId(String applicationName,String interfaceName){
        return applicationName+"@"+interfaceName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ApiData apiData = (ApiData) o;
        return getApplicationName().equals(apiData.getApplicationName()) && getInterfaceName().equals(apiData.getInterfaceName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getApplicationName(), getInterfaceName());
    }
}
